package lucene.server.web.config;

import lucene.server.common.dbmeta.ColumnMeta;
import lucene.server.core.model.FieldConfig;
import lucene.server.core.model.IndexConfig;
import lucene.server.web.dao.DynamicDao;
import lucene.server.web.dao.IndexConfigDao;
import lucene.server.web.dao.SystemDao;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Component
public class LuceneConfig {

    private static ConcurrentHashMap<String, IndexConfig> indexConfigs = new ConcurrentHashMap<>();
    /**
     * 根据索引名称获取索引配置。
     *
     * 本方法通过索引名称从全局索引配置映射中检索并返回相应的索引配置对象。如果找不到匹配的索引配置，
     * 则会返回null。使用此方法可以方便地获取特定索引的配置信息，而无需直接访问底层的配置映射。
     *
     * @param indexName 索引的名称，用于在索引配置映射中查找对应的索引配置。
     * @return 返回与索引名称匹配的索引配置对象，如果找不到则返回null。
     */
    public static IndexConfig getIndexConfig(String indexName) {
        return indexConfigs.get(indexName);
    }
    public static List<IndexConfig> getIndexConfigs() {
        return indexConfigs.values().stream().collect(Collectors.toList());
    }
    public static void createConfigs() throws SQLException {
        List<IndexConfig> indexConfigList = (List<IndexConfig>) SystemDao.getAll("index_config", IndexConfig.class);

        for (IndexConfig indexConfig : indexConfigList){
            createFieldConfig(indexConfig);
            indexConfigs.put(indexConfig.getName(),indexConfig);
        }
    }
    public static void reCreateConfig(String indexName) throws SQLException {

        IndexConfig indexConfig =   IndexConfigDao.getByName(indexName);
        createFieldConfig(indexConfig);
        indexConfigs.put(indexConfig.getName(),indexConfig);
    }
    public static void removeConfig(String indexName)  {
        indexConfigs.remove(indexName);
    }
    public static void clearConfigs()  {
        indexConfigs.clear();
    }
    public static void createFieldConfig(IndexConfig indexConfig) {
        indexConfig.getFieldsConfig().addAll(IndexConfigDao.getFields(indexConfig));
        //List<ColumnMeta> allList = DynamicDao.getFields(indexConfig.getConn(),indexConfig.getTable(),SystemConfig.getDealBlob());

        /*
        if(!"*".equals(indexConfig.getFields())){
            Set<String> fields = new HashSet<>(Arrays.asList(indexConfig.getFields().split(",")));
            allSelectList = allList.stream()
                    .filter(entry -> fields.contains(entry.getKey()))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        }else{
            allSelectList = allList;
        }

        allSelectList.forEach((k,v)->{
            if(!configedList.stream().anyMatch(fieldConfig -> fieldConfig.getFieldName().equals(k))){
                FieldConfig fieldConfig = new FieldConfig();
                fieldConfig.setFieldName(k);//字段名称不需要修改
                fieldConfig.setName(k);
                fieldConfig.setIndexName(indexConfig.getName());
                fieldConfig.setTypeName(v);
                configedList.add(fieldConfig);
            }
        });
        */
    }

}
